/**
 * 
 */
package com.blog.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.mapper.SysRoleMapper;
import com.blog.mapper.SysUserRoleMapper;
import com.blog.pojo.SysRole;
import com.blog.pojo.SysRoleExample;
import com.blog.pojo.SysUser;
import com.blog.pojo.SysUserRole;
import com.blog.pojo.SysUserRoleExample;
import com.blog.service.SysRoleService;
import com.blog.service.SysUserService;

/**
 * @author zyj
 *
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
	
	@Autowired
	private SysUserService SysUserService;
	
	@Autowired
	private SysUserRoleMapper SysUserSysRoleMapper;
	
	@Autowired SysRoleMapper SysRoleMapper;

	@Override
	public Set<String> listSysRoleNames(String SysUserName) {
		Set<String> result = new HashSet<>();
		List<SysRole> SysRoles = listSysRoles(SysUserName);
		for (SysRole SysRole : SysRoles) {
			result.add(SysRole.getName());
		}
		return result;
	}

	@Override
	public List<SysRole> listSysRoles(String SysUserName) {
		List<SysRole> SysRoles = new ArrayList<>();
        SysUser SysUser = SysUserService.getByName(SysUserName);
        if(null==SysUser)
            return SysRoles;
         
        SysRoles = listSysRoles(SysUser);
        return SysRoles;
	}

	@Override
	public List<SysRole> listSysRoles(SysUser sysUser) {
		List<SysRole> sysRoles = new ArrayList<>();
		 
        SysUserRoleExample example = new SysUserRoleExample();
         
        example.createCriteria().andUserIdEqualTo(sysUser.getId());
        List<SysUserRole> sysUserRoles= SysUserSysRoleMapper.selectByExample(example);
         
        for (SysUserRole sysUserRole : sysUserRoles) {
            SysRole sysRole=SysRoleMapper.selectByPrimaryKey(sysUserRole.getRoleId());
            sysRoles.add(sysRole);
        }
        return sysRoles;
	}

	@Override
	public List<SysRole> list() {
		SysRoleExample example =new SysRoleExample();
        example.setOrderByClause("id desc");
        return SysRoleMapper.selectByExample(example);
	}

	@Override
	public void add(SysRole SysRole) {
		SysRoleMapper.insert(SysRole);
	}

	@Override
	public void delete(Long id) {
		SysRoleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public SysRole get(Long id) {
		return SysRoleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(SysRole SysRole) {
		SysRoleMapper.updateByPrimaryKeySelective(SysRole);
	}

}
